package com.sp.system.user.utils.context;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;

@Component
public class CurrentUser {

    public static String getUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = "";
        if(authentication != null){
            userId =   (String) authentication.getCredentials();
        }
        return  userId;
    }

    public static String getUserName(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = "";
        if(authentication != null && authentication.getPrincipal() instanceof User){
            User user = (User) authentication.getPrincipal();
            userName = user.getUsername();
        }
        return userName;
    }
}
